package com.hunttown.mes.service;

import com.hunttown.mes.common.cache.Cache;
import com.hunttown.mes.common.keys.KeyConstants;
import com.hunttown.mes.common.utils.FastJsonUtils;
import com.hunttown.mes.common.utils.GeneralUtils;
import com.hunttown.mes.common.utils.ScriptFilterUtils;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.IotRegionDao;
import com.hunttown.mes.domain.IotRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * created by wangjunfu on 2021-02-25 10:58:54
 */
@Service
public class IotRegionService {

    private final static Logger logger = LoggerFactory.getLogger(IotRegionService.class);

    private final IotRegionDao iotRegionDao;
    private final AHelpService helpService;
    private final Cache jedisCache;

    @Autowired
    public IotRegionService(IotRegionDao iotRegionDao, AHelpService helpService, Cache jedisCache) {
        this.iotRegionDao = iotRegionDao;
        this.helpService = helpService;
        this.jedisCache = jedisCache;
    }

    /**
     * 新增
     *
     * @param iotRegion 实体对象
     * @return 最新插入id
     */
    public IotRegion insert(IotRegion iotRegion) {
        //api接口传进来是有值的
        if (iotRegion.getEntId() == null)
            iotRegion.setEntId(helpService.getEnterpriseId());
        if (iotRegion.getAdminId() == null)
            iotRegion.setAdminId(0);
        if (iotRegion.getCreateTime() == null)
            iotRegion.setCreateTime(new Date());
        if (iotRegion.getStopFlag() == null)
            iotRegion.setStopFlag(0);
        if (iotRegion.getDeleteFlag() == null)
            iotRegion.setDeleteFlag(0);

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(iotRegion), IotRegion.class))) {
            return iotRegion;
        }
        return iotRegionDao.insert(iotRegion);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param iotRegion 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(IotRegion iotRegion) {
        if (iotRegion.getEntId() == null) {
            iotRegion.setEntId(helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForDomain(GeneralUtils.ergodicModel(FastJsonUtils.toJSONString(iotRegion), IotRegion.class))) {
            return false;
        }
        return iotRegionDao.updateInfoById(iotRegion);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }

        if (ScriptFilterUtils.xssFilterForQuery(query)) {
            return false;
        }
        return iotRegionDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public IotRegion selectById(Integer id) {
        return iotRegionDao.selectById(id, helpService.getEnterpriseId());
    }

    public IotRegion selectById(Integer id, Integer entId) {
        return iotRegionDao.selectById(id, entId);
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public IotRegion selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<IotRegion> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<IotRegion> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return iotRegionDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return iotRegionDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return iotRegionDao.deleteById(id, helpService.getEnterpriseId());
    }

    //region //理论上缓存都是配置在应用层，这里配置一个只读缓存。

    /**
     * 先从缓存中获取数据
     * 如果缓存为空，从数据库中获取，并将获取的数据添加到缓存。
     *
     * @param id 编号ID
     * @return dto
     */
    public IotRegion getFromCacheId(Integer id, Integer entId) {
        if (id == null || id <= 0) {
            return null;
        }

        IotRegion obj = jedisCache.oget(KeyConstants.IOT_REGION_INFO_KEY_ID + id, IotRegion.class);
        if (obj == null) {
            if (entId == null || entId <= 0) {
                obj = selectById(id, entId);
            } else {
                obj = selectById(id);
            }

            if (obj != null) {
                jedisCache.oset(KeyConstants.IOT_REGION_INFO_KEY_ID + id, obj);
            }
        }

        return obj;
    }
    //endregion

}